import api from "@/utils/request";

// 获取指定 Bucket 的指定路径下的所有文件与文件夹列表
export function getAllFilesByBucketName(bucket: string, folderName: string) {
    return api.get('/documentManager/tokenFile/getAllFilesByFolder?bucket=' + bucket + '&folderPath=' + folderName)
}

// 获取指定 Bucket 的 文件Url
export function getBucketFileUrl(bucket: string, fileName: any) {
    return api.get('/documentManager/tokenFile/url?bucket=' + bucket + '&fileName=' + fileName)
}

//  获取指定 Bucket 的纯文本文件内容
export function getPureTextContent(bucket: string, fileName: any) {
    return api.get('/documentManager/tokenFile/getText?bucket=' + bucket + '&fileName=' + fileName)
}

// 上传文件 到指定 Bucket
export function uploadFileToBucket(bucket: string, path: string, formData: any, config: any, fileName: string | null) {
    return api.post('/documentManager/tokenFile/upload?bucket=' + bucket + '&path=' + path + '&fileName=' + fileName, formData, config)
}

// 删除指定 Bucket 中的文件或者文件夹
export function deleteFileOrFolder(bucket: string, path: string, type: number) {
    if (type === 0) {
        return api.post('/documentManager/tokenFile/delete?bucket=' + bucket + '&path=' + path)
    } else if (type === 1) {
        return api.post('/documentManager/tokenFile/deleteFolder?bucket=' + bucket + '&path=' + path)
    }

}


// 重命名指定 Bucket 中的文件名
export function renameBucketFile(bucket: string, oldName: any, newName: any) {
    return api.post('/documentManager/tokenFile/renameFile?bucket=' + bucket + '&oldName=' + oldName + '&newName=' + newName)
}

// 重命名指定 Bucket 中的文件夹名
export function renameBucketFolder(bucket: string, oldPath: any, newPath: any) {
    return api.post('/documentManager/tokenFile/renameFolder?bucket=' + bucket + '&oldPath=' + oldPath + '&newPath=' + newPath)
}

// 在指定 Bucket 中创建文件
export function newFileInBucket(bucket: string, fileName: any) {
    return api.post('/documentManager/tokenFile/newFile?bucket=' + bucket + '&fileName=' + fileName)
}

// 在指定 Bucket 中创建文件夹
export function newFolderInBucket(bucket: string, folderName: any) {
    return api.post('/documentManager/tokenFile/newFolder?bucket=' + bucket + '&folderName=' + folderName)
}

// 移动文件到指定目录
export function moveBucketFile(data: any) {
    return api.post('/documentManager/tokenFile/moveFile', data)
}

export async function downloadBucketFile(bucket: string, fileName: string) {
    try {
        api.get('/documentManager/tokenFile/download?bucket=' + bucket + '&fileName=' + fileName, {responseType: 'blob'})
            .then((res) => {
                const url = URL.createObjectURL(new Blob([res.data]));
                const link = document.createElement('a');
                link.href = url;
                link.setAttribute('download', fileName); // 或者你想要设置的下载文件名
                document.body.appendChild(link);
                link.click();
                link.remove();
            })

    } catch (error) {
        console.error("There was an error downloading the file!", error);
    }
}

// 上传文档url到指定Bucket
export function uploadFileViaUrl(data: any) {
    return api.post('/documentManager/tokenFile/upload-from-url', data)
}


// -------------------------------大文件上传相关----------------------

export function checkChunks(data: any) {
    return api.post('/documentManager/tokenFile/check', data)
}

export function uploadChunks(data: any) {
    return api.post('/documentManager/tokenFile/upload/chunk', data)
}

export function mergeChunks(data: any) {
    return api.post('/documentManager/tokenFile/merge', data)
}

